﻿using System;
using System.IO;
using System.Diagnostics;
namespace DavidLog
{
    /// <summary>
    /// 日志管理类
    /// </summary>
    public class LogManager
    {
        private string logPath = string.Empty;
        private LogFile logLevel = LogFile.fatal;
        private string levelstring = System.Configuration.ConfigurationManager.AppSettings["loglevel"];
        private string threadId = string.Empty;
        private string allPath = string.Empty;


        public LogManager(string threadId)
        {
            this.threadId = threadId;
        }


        /// <summary>
        /// 自定义打印级别  Warning,DeBug,Error,Run
        /// </summary>
        internal LogFile LogLevel
        {
            get
            {
                if (string.IsNullOrEmpty(levelstring))
                {
                    return LogFile.fatal;
                }
                else
                {
                    int levelint = Convert.ToInt32(levelstring);
                    switch (levelint)
                    {
                        case 0:
                            return LogFile.debug;
                        case 1:
                            return LogFile.info;
                        case 2:
                            return LogFile.warn;
                        case 3:
                            return LogFile.error;
                        default:
                            return LogFile.fatal;
                    }
                }
            }
            set { logLevel = value; }
        }

        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        private string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                    allPath = AppDomain.CurrentDomain.BaseDirectory + @"Log\all\";
                    if (!Directory.Exists(allPath))
                    {
                        Directory.CreateDirectory(allPath);
                    }
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private string logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        private string LogFielPrefix
        {
            get
            {

                if (string.IsNullOrEmpty(logFielPrefix))
                {
                    logFielPrefix = "WebSiteLog";
                }
                return logFielPrefix;
            }
            set { logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        private void WriteLog(string logFile, string msg)
        {
            string _path = LogPath;
            if (!string.IsNullOrEmpty(msg))
            {
                LogFile lf = LogFile.error;
                switch (logFile)
                {
                    case "debug":
                        lf = LogFile.debug;
                        _path += @"debug\";
                        break;
                    case "info":
                        lf = LogFile.info;
                        _path += @"info\";
                        break;
                    case "warn":
                        lf = LogFile.warn;
                        _path += @"warn\";
                        break;
                    case "error":
                        lf = LogFile.error;
                        _path += @"error\";
                        break;
                    default:
                        lf = LogFile.fatal;
                        _path += @"fatal\";
                        break;
                }

                if (lf >= LogLevel)
                {
                    if (!Directory.Exists(_path))
                    {
                        Directory.CreateDirectory(_path);
                    }
                    string msgstring = threadId + "  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + logFile + "  " + msg;
                    Writer.WriteFile(_path + LogFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".txt", msgstring);
                    Writer.WriteFile(allPath + LogFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".txt", msgstring);
                }

            }
        }

        /// <summary>
        /// 数据库sql语句处理信息   页面测试语句
        /// </summary>
        /// <param name="msg"></param>
        public void Debug(string msg)
        {
            WriteLog("debug", msg);
        }
        /// <summary>
        /// 页面流程脚本
        /// </summary>
        /// <param name="msg"></param>
        public void Info(string msg)
        {

            WriteLog("info", msg);

        }
        /// <summary>
        /// 一般空的异常情况
        /// </summary>
        /// <param name="msg"></param>
        public void Warn(string msg)
        {

            WriteLog("warn", msg);

        }
        /// <summary>
        /// 数据库连接失败  通信失败
        /// </summary>
        /// <param name="msg"></param>
        public void Error(string msg)
        {

            WriteLog("error", msg);

        }
        /// <summary>
        /// 程序启动失败
        /// </summary>
        /// <param name="msg"></param>
        public void Fatal(string msg)
        {

            WriteLog("fatal", msg);

        }

    }
    /// <summary>
    /// 日志类型
    /// </summary>
    internal enum LogFile
    {
        debug = 1,
        info = 2,
        warn = 3,
        error = 4,
        fatal = 5
    }
}
